package cn.iocoder.yudao.module.cmt.controller.admin.course;

import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;
import cn.iocoder.yudao.module.cmt.controller.admin.course.vo.*;
import cn.iocoder.yudao.module.cmt.dal.dataobject.course.CourseCategoryDO;
import cn.iocoder.yudao.module.cmt.service.course.CourseCategoryService;

@Tag(name = "管理后台 - 课程分类")
@RestController
@RequestMapping("/cmt/course-category")
@Validated
public class CourseCategoryController {

    @Resource
    private CourseCategoryService courseCategoryService;

    @PostMapping("/create")
    @Operation(summary = "创建课程分类")
    @PreAuthorize("@ss.hasPermission('cmt:course-category:create')")
    public CommonResult<Long> createCourseCategory(@Valid @RequestBody CourseCategorySaveReqVO createReqVO) {
        return success(courseCategoryService.createCourseCategory(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新课程分类")
    @PreAuthorize("@ss.hasPermission('cmt:course-category:update')")
    public CommonResult<Boolean> updateCourseCategory(@Valid @RequestBody CourseCategorySaveReqVO updateReqVO) {
        courseCategoryService.updateCourseCategory(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除课程分类")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('cmt:course-category:delete')")
    public CommonResult<Boolean> deleteCourseCategory(@RequestParam("id") Long id) {
        courseCategoryService.deleteCourseCategory(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除课程分类")
    @PreAuthorize("@ss.hasPermission('cmt:course-category:delete')")
    public CommonResult<Boolean> deleteCourseCategoryList(@RequestParam("ids") List<Long> ids) {
        courseCategoryService.deleteCourseCategoryListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得课程分类")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('cmt:course-category:query')")
    public CommonResult<CourseCategoryRespVO> getCourseCategory(@RequestParam("id") Long id) {
        CourseCategoryDO courseCategory = courseCategoryService.getCourseCategory(id);
        return success(BeanUtils.toBean(courseCategory, CourseCategoryRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得课程分类分页")
    @PreAuthorize("@ss.hasPermission('cmt:course-category:query')")
    public CommonResult<PageResult<CourseCategoryRespVO>> getCourseCategoryPage(@Valid CourseCategoryPageReqVO pageReqVO) {
        PageResult<CourseCategoryDO> pageResult = courseCategoryService.getCourseCategoryPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, CourseCategoryRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出课程分类 Excel")
    @PreAuthorize("@ss.hasPermission('cmt:course-category:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportCourseCategoryExcel(@Valid CourseCategoryPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<CourseCategoryDO> list = courseCategoryService.getCourseCategoryPage(pageReqVO).getList();
        ExcelUtils.write(response, "课程分类.xls", "数据", CourseCategoryRespVO.class,
                        BeanUtils.toBean(list, CourseCategoryRespVO.class));
    }

}
